package com.treasure.service.user.controller;

import com.treasure.service.annotation.LogInfo;
import com.treasure.service.annotation.NoLogin;
import com.treasure.service.commons.lang.util.StringUtil;
import com.treasure.service.dto.PrepareResult;
import com.treasure.service.enums.CodeEnum;
import com.treasure.service.third.utils.SmsUtils;
import com.treasure.service.third.utils.TokenConfig;
import com.treasure.service.third.utils.UserToken;
import com.treasure.service.result.ResponseResult;
import com.treasure.service.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
* @Description:    用户基本信息维护
* @Author:         panxinwen
* @CreateDate:     2018/11/30 23:50
* @Version:        1.0
*/
@Api(value = "用户基本信息维护",description = "用户个人信息维护，包括微信信息，实名信息，个人基本信息")
@RestController
@RequestMapping("/user/v1")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private SmsUtils smsUtils;

    @ApiOperation(value = "购买会员（预购买）", notes = "购买会员（预购买）")
    @LogInfo(log = "购买会员（预购买）",isPrint = true)
    @PostMapping("/dredgeMember")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "memberGrade", value = "会员等级 1-创业者 2-创业家 3-合伙人", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "couponId", value = "优惠券编号", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "payType", value = "支付方式(1:微信支付)", required = false, dataType = "int", paramType = "query")
    })
    public ResponseEntity dredgeMember(
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "memberGrade", required = false) Integer memberGrade,
            @RequestParam(value = "couponId", required = false) Long couponId,
            @RequestParam(value = "payType", required = false,defaultValue = "2") Integer payType
    ){

        TokenConfig.getTokenByHeader();

        if (StringUtil.isEmpty(userId)){
            userId = TokenConfig.getTokenByHeader().getUserId();
        }

        PrepareResult prepareResult = userService.dredgeMember(userId,memberGrade,couponId,payType,StringUtil.getIpAddress());

        return ResponseEntity.ok(new ResponseResult(CodeEnum.SUCCESS,prepareResult));
    }

    @ApiOperation(value = "获取用户基本信息", notes = "获取用户基本信息")
    @LogInfo(log = "获取用户基本信息",isPrint = true)
    @PostMapping("/getUserInfo")
    public ResponseEntity getUserInfo(){

        UserToken userToken = TokenConfig.getTokenByHeader();
        Map map = userService.getMyCenter();
        if(map.get("weekAmount")==null) {
        	userToken.setWeekAmount("0.00");
        }else {
        	userToken.setWeekAmount(map.get("weekAmount").toString());
        }
        
        if(map.get("totalAmount")==null) {
        	userToken.setTotalAmount("0.00");
        }else {
        	userToken.setTotalAmount(map.get("totalAmount").toString());
        }
        return ResponseEntity.ok(new ResponseResult(CodeEnum.SUCCESS,userToken));
    }

    @LogInfo(log = "个人中心",isPrint = true)
    @PostMapping("/getMyCenter")
    public ResponseEntity getMyCenter(){

        Map map = userService.getMyCenter();

        return ResponseEntity.ok(new ResponseResult(CodeEnum.SUCCESS,map));
    }

    @NoLogin
    @LogInfo(log = "注册、登录或绑定手机号",isPrint = true)
    @ApiOperation(value = "注册、登录或绑定手机号", notes = "注册、登录或绑定手机号")
    @PostMapping("/registerAndLogin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phonenumber", value = "用户手机号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "inviteCode", value = "邀请码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "authCode", value = "短信验证码", required = true, dataType = "String", paramType = "query")
    })
    public ResponseEntity registerAndLogin(@RequestParam(value = "phonenumber") String phonenumber,
                                           @RequestParam(value = "inviteCode",required = false) String inviteCode,
                                           @RequestParam(value = "authCode") String authCode){
        ResponseResult responseResult =  smsUtils.verify(phonenumber,1,authCode,null);
        if ("200".equals(responseResult.getCode())){
            responseResult = userService.registerAndLogin(phonenumber,authCode,inviteCode);
        }
        return ResponseEntity.ok(responseResult);
    }

    @LogInfo(log = "完善个人信息",isPrint = true)
    @ApiOperation(value = "完善个人信息", notes = "完善个人信息")
    @PostMapping("/perfectInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardNumber", value = "身份证号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "cardName", value = "姓名", required = false, dataType = "String", paramType = "query")
    })
    public ResponseEntity perfectInfo(@RequestParam(value = "cardNumber") String cardNumber,
                                      @RequestParam(value = "cardName")   String cardName){
        ResponseResult result = userService.perfectInfo(cardNumber,cardName);
        return ResponseEntity.ok(result);
    }

    @NoLogin
    @LogInfo(log = "落地页注册接口",isPrint = true)
    @ApiOperation(value = "落地页注册", notes = "落地页注册")
    @PostMapping("/registerForFall")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phonenumber", value = "用户手机号", required = true, dataType = "String", paramType = "query")
    })
    public ResponseEntity registerForFall(@RequestParam(value = "phonenumber") String phonenumber,
                                           @RequestParam(value = "inviteCode",required = false) String inviteCode){
        ResponseResult responseResult = userService.registerAndLogin(phonenumber,null,inviteCode);
        return ResponseEntity.ok(responseResult);
    }

}
